
import delimited using health/rdrp3367_covid_de_v5.csv, clear asdouble


duplicates tag, gen(dupe)
tab dupe
assert dupe == 0

gen date = date(substr(lab_date,1,9), "DMY")
sort sid date
by sid: gen tid = _n == 1
by sid: gen nTests = _N
count
count if tid

gen positive = lab_result == "Positive"
gen negative = lab_result == "Negative"
by sid: egen posTest = max(positive)
by sid date: gen tag_person_day = _n==1
by sid date: egen pos_that_day = max(positive)
by sid date: egen neg_that_day = max(negative)


label var posTest "Ever had a positive test"
local var nTests "Total times tested" 
save health/test_data_all, replace 

** person level version
by sid: keep if _n == 1
save health/test_person_level, replace 


********************************************************************************
********************************************************************************
** make a daily panel of tests / positives
********************************************************************************
********************************************************************************

use health/test_data_all
collapse (max) positive, by(sid date) 
gen tested = 1 
gen negative = 1-positive
compress
sort sid date
assert (positive | negative) if tested
save health/test_daily_panel,  replace
